package com.qihetimes.earthangel.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qihetimes.earthangel.common.ConfigType;
import com.qihetimes.earthangel.mapper.ConfigMapper;
import com.qihetimes.earthangel.model.Config;
import com.qihetimes.earthangel.service.IConfigService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 配置表 服务实现类
 * </p>
 *
 * @author f4cklangzi@gmail.com
 * @since 2022-01-12
 */
@Service
public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, Config> implements IConfigService {
    
    @Override
    public ConfigType.IntegralConfig getIntegralConfig() {
        ConfigType.IntegralConfig integralConfig;
        Config config = lambdaQuery().eq(Config::getConfigType, ConfigType.INTEGRAL).one();
        if (config == null) {
            config = new Config();
            config.setId(IdUtil.objectId());
            config.setConfigType(ConfigType.INTEGRAL.name());
            integralConfig = new ConfigType.IntegralConfig();
            integralConfig.setUploadGoods(20);
            integralConfig.setSendGoods(80);
            integralConfig.setGetGoods(20);
            integralConfig.setSendGoodsOriginalPriceRatio(0.1D);
            integralConfig.setShareGoods(5);
            List<Integer> list = Arrays.asList(0, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000);
            integralConfig.setLeveIntegralList(list);
            config.setConfigContent(JSONUtil.toJsonStr(integralConfig));
            save(config);
            return integralConfig;
        }
        return JSONUtil.toBean(config.getConfigContent(), ConfigType.IntegralConfig.class);
    }
}
